This Web Design course will teach you HTML, HTML 5, and Cascading Stylesheeyts (CSS). It is aimed at complete beginners to the subject, and assumes you have no knowledge of creating web pages. (If you were doing the old course, it has now been moved here: old course.) You won't need to buy any expensive software for this course. To see what you WILL need, simply click the Course Software link below. The course has been designed to work on just about any operating system.
TABLE OF CONTENTS
Section One: Getting Started
1. Course Introduction
2. Course software
3. What is a HTML and HTML 5?
4. Getting started with tags
5. How to save web pages
6. Viewing your web pages
Section 2: Basic HTML Tags
1. Basic HTML template
2. Heading Tags
3. Paragraph and Break tags
4. Bold and Italics
5. HTML lists
Reference: Basic HTML Tags
Section 3: Getting started with CSS
1. Introduction to CSS
2. CSS rules
3. Where to put your styles
4. Using CSS selectors
5. Inline and Embedded styles
6. CSS and Fonts
7. Font colours
8. Font sizes
9. Styling Fonts
Reference: CSS and Fonts
Section 4: Dealing with Images
1. Types of Images
2. Inserting Images part 1
3. Inserting Images part 2
4. Image Attributes
5. Images and CSS
6. Text wrapping with CSS
7. CSS and image borders
8. Background Images
9. Adding captions to images
Reference: CSS in this section
Section 5: Linking to other pages
1. Hyperlinks
2. Linking to other pages
3. Other types of hyperlinks
4. CSS and hyperlinks
5. External stylesheets
6. HTML lists and nav bars
Reference: CSS in this section
Reference: HTML in this section
Section 6: CSS Layouts
1. The Box Model
2. CSS Comments
3. CSS Positioning
4. CSS floats
5. A one column CSS layout
6. Styling the one column layout
7. Styling HTML 5 tags
8. A two column CSS layout
Reference: CSS in this section
Reference: HTML5 layout tags
Section 7: HTML 4 and HTML 5 tables
1. HTML 4 Tables
2. Row and Col span
3. Table alignment, colour, images
4. HTML 5 Tables
Reference: HTML and HTML5 tables
Section 8: HTML forms
1. Form Tags
2. Textboxes, Submit, Reset
3. Formatting Textboxes with CSS
4. Labels, Textareas
5. Option buttons and Checkboxes
6. Passwords, hidden fields
Reference: HTML and HTML5 Forms
Section 9; HTML5 and Forms
1. HTML 5 Forms
2. Placeholders, Required
3. Email, URL, Search
4. Spinners and Sliders
5. Dates, Colour pickers
6. Data Lists
7. HTML Form Layouts
8. HTML 5 Video and Audio
9. The HTML 5 Canvas tag
10. Details, Aside, Mark
Reference: HTML5 Audio and Video
Section 10: Getting your site on the internet
1. Websites and Domain Names
2. What to look for when buying Webspace
3. Search engine optimisation
4. Pay per click advertising
5. Sitemaps
Section 11: HTML and CSS Reference
1. Basic HTML Tags
2. CSS Font and Text Properties
3. CSS Borders, Margins, Padding
4. CSS used in the Lists section
5. CSS used in the Positioning section
6. HTML images and hyperlinks
7. HTML5 Layout tags
8. HTML and HTML5 Table tags
9. HTML and HTML5 Form tags
10. HTML5 Video and Audio tags
11. HTML Special Characters
Introduction to the Web Design Course
Hello, and a very warm welcome to the Home and Learn computer course for Web Design. The software you need is set out below. We assume that you have absolutely no knowledge of designing web pages. Throughout the course of this book you will learn the fundamentals of Web Design. And, of course, you will start creating your own pages. By the end of the book, you will have acquired a good understanding of what web design is all about, and have the ability to take it further, if you so wish.
What you will learn
The aim of this course is to get you started designing web pages. It is assumed that you have little or no experience of the subject. During our time together, you'll learn some new concepts and ideas, most of which will not be difficult. After all, there are billions of pages on the internet, designed by the whole spectrum of humanity: children, adults, pensioners, people from all walks of life have sites out there. And if they can do it, so can you. In fact, follow the lessons carefully and you WILL do it. Before long, you'll have your own pages designed and ready to be uploaded, there for all the world to see.
The technologies you will learn are HTML, HTML5, and Cascading Style Sheets. (HTML5 is the newest version of the of the language.)
At the heart of every web page is something called HTML. You will learn what this is, and how to code it. You will also learn the newest version of HTML, which is called HTML5. Both versions are included in this course. As well as HTML, you will learn about Cascading Style Sheets, and will be able to improve the look of your web pages by adding CSS to enhance them.
At the end of most chapters in this book you will see a summary table of what you have learnt. The summary tables are also included in a separate document, called quick_reference.pdf. This is one of the files that come with this book. All the files you should have are detailed below.
What you need to do the course
The only thing you really need to do the course is a simple text editor. We explain all about this in the first chapter, Anatomy of a Web Page, in the section Software for Writing Web Pages. But the operating system you have is not important. So you can do this course on a PC or an Apple Mac.
Web Design Course Files
There are a number of files that you will need in order to complete certain sections. Whenever you need a file for a section of your course, it will be explained in the relevant section. The download location is here, under the heading Web Design - New Course (you don't need the downloads for the old course.):
Software for Writing Web Pages
To start writing web pages, we'll use a simple text editor. That way, you'll be able to focus on the code, without getting distracted by the menus and buttons of a complex piece of Web Design software.
If you use Windows, then Notepad is an excellent text editor to use. To launch it, click on your Start menu in the bottom left of your screen. Click on All Programs, then Accessories. From the Accessories folder, select Notepad:
The software should look like this, when it launches:
Another good piece of software is the free Notepad++. You can get a copy here:
Notepad Plus
Mac and Linux users have a number of text editors available, but finding a free one that is as good as Notepad++ is quite difficult. Bluefish is OK, but looks a bit dated, especially for HTML. You can check it out here:
Bluefish
The free Smultron is worth downloading, if you have a Mac. You can grab a copy here:
Smultron
For a basic text editor like Notepad, Macs come with TextEdit, which you can find in your Applications folder. You might want to play around with the Preferences, though, or you'll end up with Rich Text rather than Plain Text.
Another good free editor for the Mac is TextWrangler. You can get a copy here:
Text Wrangler
In the next lesson, we'll take a look at just what is HTML and HTML 5.
What is a web page?
In this section, you're going to learn just what a web page is and how it can be read by a browser like Internet Explorer or Firefox. You'll also construct your very first web page. Let's make a start.
Files on your computer come with extensions. If you wrote a letter using Microsoft Word and saved it with the name 'MyLetter', the software would add four letters to the file name (three in older versions). Because it was typed using Word, the four letters that get added to your filename are .docx . So your file name will be 'MyLetter.docx' and not just 'MyLetter'. If you created a spreadsheet in Microsoft Excel and called it 'Accounts' the Excel software will add its own four letter extension to your file name. It will add .xlsx . So you file name will be 'Accounts.xlsx' and not just 'Accounts'. So you get a different four letter extension depending on the software you used.
These extensions are very important to computers. They are used to identify the type of file it is. With a file extension, Word can recognise its own documents. It sees the letters docx and says 'Ah yes, that's one of mine. I can open it.' If it sees a different extension, xlsx for example, it says 'What the heck is that?' You may then get an error message telling you that the file type is not recognised.
Web pages have their own file extensions. Oddly there are two different extensions, a three letter file extension and a four letter extension. Web pages come with the extension .htm or .html . A browser can recognise either extension.
When you open up a web page with your browser, Internet Explorer for example, the browser software checks the file extension, the same check that Word and Excel make. If it sees the .htm or .html extension it knows it's a web page and then tries to open it. (Modern browsers, though, can open up other types of files. Internet Explorer can open up your Word documents and PDF files.)
Behind the scenes, however, the thing that Internet Explorer is trying to open is really a text file that has had its extension changed from .txt to .htm . The text file though will have special symbols and words, called Tags. When the browser sees these Tags it goes to work, displaying whatever you typed and hiding the Tags from your viewers.
Most web pages on the internet are written in code called HTML. HTML stands for HyperText Markup Language, and is fairly easy to get the hang of. That's because HTML is not a programming language: it is a language designed to improve the presentation of text. For example, in Microsoft Word, if you want a nice big heading, you can select a font size from a menu. HTML has an easy way to change the size of text of headings, too, which you'll see in a moment. But that's basically all you are doing with HTML ' presenting text and images on a page. The way you do this is with things called TAGS.
What is HTML 5?
HTML comes in different versions. When people talk about HTML 5 they are talking about the updates to the mark up language. These updates were agreed (mostly) by a whole host of different parties, all members of an organisation called W3C. W3C was founded in 1994 by Tim Berners-Lee, who also created the first version of HTML and invented the World Wide Web. They now oversee the defining of new Web technologies. Comapnies who make browser like Microsoft (Internet Explorer), Mozilla (FireFox), and Apple (Safari) can then decide which of the new Web technoligies they wish to implement.
Most of the big names in browser technology decided to implement quite a lot of the new suggested updates (specifications) put forward by the W3C. These specifications are commonly known as HTML 5. In practice, this means implementing features like HTML Video and audio directly into the browser, as opposed to needing a 3rd party plugin like Adobe's Flash. Another exciting update is something called the Canvas tag. This allows you to create quite sophisticated animations and graphics using Javascipt, again without needing a 3rd-party plugin.
There are a whole lot of new HTML tags that take browser technology forward into the 21st century. Throughout this course, we'll introduce you to the essential HTML 5 tags that you need in order to create a modern web page.
HTML Tags
HTML is written in something called tags. Tags come in pairs, an opening one and a closing one. The first pair of tags we'll write are the HTML tags themselves. You put one HTML tag at the top, and one at the bottom:
<HTML>
</HTML>
This tells a browser like Internet Explorer or Firefox that the code is HyperText Markup Language.
Two things to notice here. One, that the word HTML is surrounded by angle brackets (the Less Than and Greater Than keys on your keyboard); and two that the second Tag has a forward slash before the HTML.
All your tags must be surrounded by the angle brackets < >. This tells the browser that there is some HTML code that needs executing, and that it is not to be shown on the web page. Miss an angle bracket out and it can really mess up your web page.
So add those two tags to your text editor and it will look like this:
The first Tag tells the browser to start doing something; the second tag tells the browser to stop doing it. The stop symbol is that forward slash. So miss that out and, again, your web page can really be messed up.
The next pair of tags are the HEAD tags: They go between the two HTML ones:
<HTML>
<HEAD>
</HEAD>
</HTML>
Again, notice that both the HEAD tags are surrounded by angle brackets, and there is a forward slash before the final one </HEAD>.
The HEAD section of HTML page is where you put special instructions for the browser. Your browser will not print directly to a web page when things are inserted into the HEAD section. For example, if you wanted to display the words 'Hello World' on your page, and you typed them in between the two HEAD tags, then the Browser would just ignore them. That's because 'Hello World' is direct text, and not a special instruction that the browser can understand.
A special instruction your browser can understand is the TITLE tag:
<TITLE> </TITLE>
The Title tag always goes in the HEAD section of the HTML skeleton.
The TITLE tag doesn't do very much. And it definitely should not be confused with the name of your web page. If you saved your web page as MyFamily.html then that would have no bearing on or relationship with the TITLE tag. Whatever you type between the two TITLE tags is what appears at the very top of your browser window. And that's it, that's all it does. (You'll see what it looks like in a moment.)
The final, and most important part, of the HTML skeleton is the BODY section.
In between those two BODY tags is where you'll write most of your HTML code. Think of the BODY as the white page in Word Processing. When you type something in your word processing software, it is immediately displayed. Similarly, when you type something between the two BODY tags and then view the results in a browser, whatever you typed will be displayed. As long as it's not a tag, your browser will try to display it for you. So remember: if you want people to see it, put it between the BODY tags.
In your text editor, then, add the following:
Notice that we've added some text between the two TITLE tags: My First Page . We've also used indenting, but this is only for readability: the indenting doesn't have any effect on the results.
DOCTYPE
One special tag is DOCTYPE. It's not really a HTML tag, but an instruction to the browser about which version of HTML is allowed. In version of HTML prior to HTML 5 the DOCTYPE could be very messy. In HTML 5, however, it's just this:
<!DOCTYPE HTML>
DOCTYPE needs to go at the very top of the page. There is no end tag for DOCTYPE. Note the exclamation mark after the first angle bracket, and the space between DOCTYPE and HTML.
So add a DOCTYPE to the top of your page. Your text editor will look something like this:
Coming up next, we'll take a look at how to save your web pages.
<< What is web page? | How to save your web pages >>
Bac/TITLE
Saving Web Pages
To save your code as a web page, click the File menu at the top of Notepad (or whatever Text Editor you are using.) From the File menu, select Save As . The Save As dialogue box appears (We created a new folder to store all our web pages in, and called the folder HTML)..
Note the default extension at the bottom, under 'Save as type' It says, Text Documents (*.txt) . If you leave it on that then you won't have web page, you'll have a text file. Click on the drop down list and change it to 'All files'. (You don't need to do this if you're using TextEdit on a Mac. Just type firstwebpage.html in the Save As box.)
In the File Name box, type
firstwebpage.html
Note that we've used all lowercase letters. This is not strictly necessary, but is recommended. Also, try not to add spaces to web page names as this can cause problems. You can use underscores or hyphens, though. So these two are OK:
first_web_page.html
first-web-page.html
But these two should be avoided:
first web page.html
FirstWebPage.html
Click on Save and you'll have a HTML page that can be displayed in a browser.
In the next lesson, you'll see how to view your web pages.
Viewing your web pages in a browser
To view the results, open up an explorer window. In Windows, this is done by holding down the Windows keys and then pressing the letter E on your keyboard. Or click your Start menu in the bottom left, and then click on Computer . On a Mac, click the Finder icon in the Dock.
Navigate to where you saved your HTML page. In our case, this was a folder we created called HTML, which is on our network:
In the image above we have a file called
firstwebpage.html . If yours has .txt on the end instead of .html then try again because it's not a web page but a text document.
Double click your HTML file and it will launch in your default browser. For us, this is Firefox. Yours might be Internet Explorer or Safari. You should see a blank white page with your TITLE at the top. Here's the Firefox version:
And here's the same page in Internet Explorer:
Notice that in both versions the TITLE we added is at the very top of the screen ' 'My First Page'. The same TITLE can be seen further down, at the top of each browser tab.
Exercise
Change the TITLE tag to something other than 'My First Page'. Click File > Save in your text editor. Switch back to your browser and press F5 to refresh the page. Your new TITLE should appear at the top of the screen and at the top of the browser tab.
In the next section, you'll learn how to create a HTML template.
Create a Basic HTML Template
We've produced a video to go with this lesson. It's recommended that you read the text below as well, though. The video is here:
How to Create a HTML Template
While you have the Windows Explorer or Finder screen open, you can save your current HTML skeleton as a template. That way, you don't have to type it all out every time you want a new web page.
In your text editor, delete any text between your two TITLE tags. Now click
File > Save As . From the Save As dialogue box change the name of your web page to template.txt :
Click Save to save the code as a text file. You will be returned to your text editor. Click File > Save As again. Now change the name of the file to newpage.html . In the Save As Type box in Windows, change it to All Files :
Save the file and switch to Windows Explorer or Finder. You should now see three pages:
One of these, the template is a text file rather than a HTML file. From now on, you can open the template text file, and repeat the process above: Click File > Save As , change the Save As Type box to
All Files , then type a new name for your web page, not forgetting the html ending.
There is a problem, however. If you were to double click your newpage.html file it would open up in your browser. But you will want to open it up in Notepad so that you can edit the file and make changes.
To solve the problem, you can add an item to the Send to menu in Windows (Macs will have an Open With right-click menu. Your text editor should be on there). This appears when your right-click the file:
In the image above, we have Notepad on the Send to menu. Selecting this item means that we can quickly open up the code in Notepad.
To add an item to the Send to menu, switch back to your Explorer window. In the address bar at the top, enter the following:
%APPDATA%\Microsoft\Windows\SendTo
Press the Enter key on your keyboard and you will be taken to the Send to folder:
You can now drag and drop items from your Start menu to this folder. (Make sure Windows Explorer doesn't fill the whole of your screen by clicking the Restore Down icon just to the left of the Red X.)
Click your Windows Start button. From the All Programs > Accessories menu, locate Notepad again. Hold down your left mouse button on Notepad. Keep it held down and drag across to your Windows Explorer and your Send To folder:/
Let go of your left mouse button and Notepad will be on your Send To menu when you right-click a file in Windows Explorer.
Now you can quickly open up your HTML code in Notepad and makes changes to it.
To recap this section, here are the important points again:
The HTML skeleton is the foundation on which most internet pages are based
HTML is written in Tags
Tags usually come in pairs
A Tag is a word surrounded by angle brackets, e.g.: <HTML> </HTML>, <HEAD> </HEAD>, <TITLE> </TITLE>
A pair of tags has as a starting Tag and an end Tag. The end Tag is preceded by a forward slash
Add <!DOCTYPE HTML> to the top of all your HTML pages.
In the next section, you'll see some more basic tags.
<< Viewing web pages | Heading tags >>
Basic HTML - Heading Tags
You can have a nice big heading on your web page quite easily. This is done with the H tags. The biggest size heading you can have is H1. If you want smaller sized headings then you change the number after the H. You can have up to 6 different sizes. All headings need to go between the two BODY tags.
Try it out for yourself. Open up the code for your firstwebpage.html file, if it's not already open. (If it's not, you can now right-click and Send To > Notepad . If you didn't get this working then simply click
File > Open from the menu at the top of your text editor.)
Add the following just below your first BODY tag:
<H1> A Size 1 Heading </H1>
Incidentally, tags are not case sensitive. So you can have this:
<h1> A Size 1 Heading </h1>
Or even this:
<h1> A Size 1 Heading</H1>
But using all capital letters makes your code more readable.
Once you've added the H1 tags, though, your HTML should look like this:
To see what it looks like, open Windows Explorer or Finder on the Mac, if it's not already open. Navigate to where you saved your web page to, and then double click to open it up. (If you already have the page open, simply press F5 to refresh the page.) You should see this in your browser:
Switch back to your code and change the 1's into 3's. So your HTML heading should now be this:
<H3> A Size 3 Heading </H3>
Save the changes. Go back to your browser and press F5 to refresh the page. You should see this:
Notice how much smaller the H3 heading is compared to the H1 heading. (If your headings don't look right, make sure you haven't missed any angle brackets out, or any forward-slash closing tags.)
Exercise
Try the other H numbers to see how they compare. You can only go as far as H6.
In the next lesson, we'll take a look at paragraph breaks
Paragraph and BR breaks
If you've ever used a word processor like Microsoft Word then you know that to start a new paragraph all you need to do is to hit the Enter key on your keyboard. The cursor will then move down, ready for you to start typing. In HTML, however, you can't do this. If you want to start a new paragraph, you have to use the P tags.
To try it out, add the following just below your Heading (You can use your own text, though, rather than type out the Hamlet soliloquy):
<P>To be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles, and by opposing end them? </P>
<P>To die: to sleep; no more; and by a sleep to say we end the heart-ache and the thousand natural shocks that flesh is heir to, 'tis a consummation devoutly to be wish'd.</P>
When you've finished, your HTML code should look like this: (Don't worry about the indenting, though. We did ours just so it would look nice in this book. Only one press of the spacebar is recognised in HTML, everything else is ignored, including indents and carriage returns.)
Note the P tags:
<P></P>
You have to use the P tags whenever you want to start a new paragraph.
Strictly speaking, though, you don't need the closing P tag. You can just do this to start a new paragraph:
<P>
To be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles, and by opposing end them?
<P>
To die: to sleep; no more; and by a sleep to say we end the heart-ache and the thousand natural shocks that flesh is heir to, 'tis a consummation devoutly to be wish'd.
The result is the same. But in modern web coding, it's best to use the closing tag for paragraphs, so that you can add Styling rules. (You'll learn how to do this a little later.)
Save your work and view the results in your browser. You should see this:
Notice the paragraphs breaks in the text. Notice, too, that although our code was indented, this doesn't show up in the browser. The browser will ignore all those indents we had, and any extra white space. If you want white space you have 'tell' the browser. You do this with the break tags, like P and BR (which you'll see soon).
As an exercise, try deleting the P tags in your code. Save and refresh your browser. Watch what happens:
Without the P tags the text just runs on.
There is still, however, a paragraph break after the heading, even though we deleted all the P tags. This is because the H heading tags insert their own paragraph breaks.
The BR tag
The BR tag is used when you don't want a full paragraph break. The space between lines of text is then reduced. The BR tag doesn't need a closing tag, and can be just by itself.
As an example, add the following to the end of your text (the BR part).
<P>To die: to sleep; no more; and by a sleep to say we end the heart-ache and the thousand natural shocks that flesh is heir to, 'tis a consummation devoutly to be wish'd.
<BR>
... Rest of Hamlet's Soliloquy goes here
</BODY>
</HTML>
Notice that we've deleted the end P tag for the second paragraph. If you leave it in, you'll get a double line break from the two <P> tags, as well as a single line break from the <BR> tag.
Save your changes and switch to your browser. Press F5 to refresh the page and the results should look like this:
So if you don't want a full, double-line break then remember to use the BR tag. And careful of the end P tags or you'll end up more line breaks than you expected.
In the next lesson, we'll take a look at bold and italic text.
<< Heading tags | Bold and Italic >>
Bold and Italics
Two more tags that come in useful are the Bold and Italic tags. They are quite easy to use. Here are the Bold tags:
<B> </B>
And here are the Italic tags:
<i> </i>
The text you want to change goes between the two tags:
<B> ... Rest of Hamlet's Soliloquy goes here </B>
<i> ... Rest of Hamlet's Soliloquy goes here </i>
If you want Bold and Italic text then you can nest the two:
<B><i> ... Rest of Hamlet's Soliloquy goes here </i></B>
The two I tags go between the two B tags. You can have it the other way around, though, with the I tags first:
<i><B> ... Rest of Hamlet's Soliloquy goes here </B></i>
Be careful of this, however:
<i><B> ... Rest of Hamlet's Soliloquy goes here </i></B>
This is a mismatched pair of tags. The two tags on the inside are B and I, and the ones on the outside are I and B. A modern browser will probably correct the mismatch, but older ones may not.
Try it in your HTML code:
Save and view the results in your browser:
There is also an underline tag you can use:
<U> ... Rest of Hamlet's Soliloquy goes here </U>
You can nest all three tags: bold, italics, and underline:
<U><B><i> ... Rest of Hamlet's Soliloquy goes here </i></B></U>
All this nested HTML code can be a bit messy, however. The modern solution is to use something called a Cascading Stylesheet. Using a Stylesheet allows you to do all your text formatting in the HEAD section, or better yet in an external file. (You'll see how to do all this shortly.)
In the next lesson, we'll take a look at HTML lists.
HTML Lists
You can insert a bulleted list onto your page with HTML. There are two types of lists to choose from: Ordered List and Unordered. An Ordered list is one that uses number, letters, roman numerals, or a combination. Like this:
An Unordered List is one that uses bullets. Like this:
To get a list with numbers or letters, this tag is used:
<OL>
</OL>
The OL stands for Ordered List, of course. But those two tags won't get you a list. You need to include another tag, the LI tag. LI stands for List Item. You need one pair of LI tags for every item in your list. So for three items, the code is this:
(Strictly speaking, you don't need the end LI tags.)
To get the bulleted list, the UL tag is used. UL stands for Unordered List. It's used in exactly the same way. Just substitute the OL tags for UL tags.
For both the Ordered and Unordered list, you can specify which type you want to use for the bullets or numbers. The types are these:
You use the Types like this:
You can specify a start, as well. But the start has to be a number:
So that List will be uppercase letters, starting at the 7th letter, or 'G'.
There are three types of bullets you can use for unordered lists: Disc, Circle, and Square. You use them like this:
The default is Disc. So if you want round black circles for your bullets, you can miss the TYPE attribute off:
And here are the results in a browser:
Try out the various lists in your HTML code, and add the different types to the code. That way you will get a feel for how they are used, and what they look like.
In the next section, you'll get an introduction to Cascading Stylesheets.
Some Basic HTML Tags
Tag Explanation Attribute Options
<H1>, <H2>, <H3> ... <H6> Heading Tags
<P> Start a new paragraph
<BR> Single line break
<B> Bold text
<I> Italics
<OL> Ordered List TYPE
A (Capitals)
a (Lowercase)
I (Capital Roman Numerals)
i (Lowercase Roman Numerals)
1 (Numbers)
<UL> Unordered List TYPE Circle, Disc, Square
<LI> An item for your list
An Introduction to Cascading Style Sheets
HTML was not intended to be a graphic design tool. It was set up as a simple way to display text in a browser, rather like a word processor displays text on a page. Tags were added over the years in order to add a bit of colour and life into the basic white page (actually, grey at first). So along came things like images, tables, frames, and forms. These could all be presented on the page using straight HTML code.
Web designers clamoured for a better way to present their work on a web page. Plain HTML just wasn't enough. After all, with HTML, in order to get text or an image exactly where you want it, you have to resort to complicated tables to force the alignment. And suppose you want colour behind a single paragraph of text, and not have to colour the entire page? Very tricky with straight HTML. And what about hyperlinks? Is there any way, with HTML, that we can turn the underline on and off?
These questions, and many more, were finally addressed by the introduction of Cascading Stylesheets. A Style is, basically, just another way to manipulate elements on a page, in order to bring a spark of life into your web design.
What is a Stylesheet?
If you were using a word processor like Microsoft Word, you could tell the word processor how you want blocks of text to be formatted. For example, all of your page Headings could be in 28 point Times, bold, and coloured red. If you wanted the same Heading again, you can just click a drop down list and select the Heading style you set up.
Using straight HTML, you can't do that. There's no way to apply the same formatting with a single Tag. Cascading Stylesheets, however, let you do precisely that ' change whole blocks of text with a single tag. This not only makes your code easier to read, it is also very simple to change all the formatted blocks of text to say a different font size or font colour.
For example, in HTML, if you want to set the first paragraph of every page to bold and italics, you'd have to do this for every single paragraph that needs it:
<P>
<B><i>This is the first paragraph on page One. The same font styles are needed for each page on my web site.</i></B>
</P>
With Stylesheets, you can get rid of all that code, and place it in the HEAD section of your page. You would then just apply the Style to any paragraph that needs it. Like this:
<P Class = "FirstParagraph">
This is the first paragraph on page one. The same font styles are needed for each page on my web site.
</P>
The new code, I'm sure you'll agree, looks much cleaner. And if you decided that the text colour should be blue, you can just make one change to your Stylesheet code and all the first paragraphs of your pages would change.
A stylesheet is set up by using the word STYLE in between two angle brackets. An end STYLE tag is needed to tell the browser to stop formatting the style:
<STYLE>
</STYLE>
Your stylesheet code then goes between the two Style tags. Here's a style that can change text blue:
<STYLE>
.Font1 { Color: Blue }
</STYLE>
<P Class =" Font1">
This is my text.
</P>
Although you may not understand the code layout yet, the point is that you can add other styles to the one above that we have called Font1. We can add a bold style and a size style:
<STYLE>
.Font1 {
Color: Blue;
Font-size: 24pt;
Font-weight: Bold;
}
</STYLE>
Now the part of the code where we applied the style (P Class = Font1) will have its text updated. We don't have to make any changes at all to the P part of the code.
So a style is way to change blocks of code (or even individual words) and apply formatting to the block as a whole. You don't need individual HTML tags in the BODY of your page; just using the style name once will ensure that your formatting is applied to the whole block.
In the next lesson, you'll learn about CSS Rules.
Cascading Style Sheet Rules
We've produced a video to go with this lesson. It's recommended that you read the text below as well, though. The video is here:
Cascading Stylesheet Rules
A Cascading Stylesheet rule tells the browser what the HTML looks like, and what it should do. A rule can dictate what just one HTML tag should look like, or you can construct your own rule to be applied as and where you want it.
For example, a rule can be set up that tells the browser to format every <P> tag so that its first line is indented. Or you could construct your own paragraph rule, and just apply the style to certain paragraphs, not all paragraphs.
There are three parts to a Rule: The Selector, the Property, and the Value.
The Selector
There are three different kinds of CSS Selector: An HTML selector, a Class selector, and an ID selector.
An HTML Selector is the text part of an HTML tag. The complete paragraph tag is <P>. So its Selector is just P ' in other words, strip the angle brackets off and you get the HTML Selector.
A Class Selector is one you set up yourself, to be used anywhere on your page. The Font1 from our STYLE example above was a Class Selector. We picked the name ourselves and then applied the style to some text on the page.
An ID Selector is similar to a Class selector, but you use them to identify a particular element, a text box element on a form, for example.
Here's an example of what all three selectors look in a STYLE tag.
The first one, H1 , is the HTML Selector. Notice that it has had its angle brackets removed. With an HTML selector, all the HTML tags on the page will be formatted in the style you have set. So for H1 above, all the text between the <H1></H1> tags on the page will now be in Red.
The second one, .NewFont , is the Class selector. Note that a class selector must start with a full stop (period). Then you type the name for your selector (anything you want). No space is added between the full stop and the name of your selector.
The third one, #NewTextboxColour, is the ID selector. An ID selector starts with the hash/pound (#) symbol. You then type the name you want to use for your ID selector. Again, no space is added between the symbol and the name of your selector.
Property and Value
Once you have set up your Selector, you then define the Properties and Values for that selector.
The Property for the selector is the thing you're trying to change. Examples are: Font, Color, Background, Margin, Text.
The Value for the selector is the new setting for the property. For example, for our COLOR property, we can set it to a value of an actual colour (red, blue, yellow), or a colour code (#FFFF00, #000000).
The property and the value are enclosed in curly brackets { }. The syntax for the whole thing would then be:
Selector {Property: Value}
An example is:
H1 {Color: Red}
H1 is the selector, Color is the property, and Red is the value of the property. Note the colon ( : ) after the Property. This is used to separate a Property from a Value, so that the browser knows which one is which.
If you want to add more than one property and value, there are two way to do it: all on one line, with each pair of properties and values separated by a semi-colon ( ; ). Or you can put each pair of properties and values on multiple lines separated by a semi-colon ( ; ). Like this:
H1 {Color: Red; Font-weight: Bold; Font-Size: 16pt;}
The multiple lines version is this:
The multiple lines version is easier to read.
So, to sum up:
A CSS rule has three parts, a Selector, a Property, and a Value
The Selector can be a HTML selector, a Class selector, or an ID selector
You separate the Property and Value from the Selector by enclosing them in curly brackets, a left curly bracket first { and a right curly bracket to close the rule }
A Property is separated from a Value by a colon ( : )
If you're using more than one pair of properties and values for the same selector, separate them with semi-colons ( ; )
In the next lesson, you'll see where to put your styles.
<< Introduction to CSS | Where to put your styles >>
Where to put your styles
STYLES can be inserted into three locations: Inline, Embedded, and External.
Inline Style Sheets
You can place a style tag directly in a HTML Tag. This is called Inline. Inline styles will override ones placed elsewhere. Here's an example of an Inline style:
<H1 STYLE = 'Color: Red'> My Heading </H1>
To place a style in a HTML tag, do the following:
Type the Tag you want to change
Next, type a space and then the word STYLE
Type an equals sign ( = ) after the word STYLE
Type a double quote mark
Type the Property followed by a colon
Type the Value
Type the another double quote mark
Type the right angle bracket ( > ) of the HTML tag
Embedded Style Sheets
Embedded styles go in the HEAD section of your HTML page. When you embed a style in the HEAD section, you use the two tags to tell the browser where the style starts and ends. You can add a TYPE attribute, if you want. But modern browsers don't need it.
Then in between the two STYLE tags, you would type your CSS Rules.
External Style Sheets
Instead of typing the <STYLE> tags and the code for all your CSS rules in the HEAD section, you can type it all in a separate text file. You then 'tell' the browser where the text file is. The text file (along with its code) is then treated as though it were in the HEAD section. You set up an External stylesheet like this:
To embed a stylesheet the LINK tag is used. The REL attribute tells the browser that you want to link to a stylesheet; the TYPE tells the browser what sort of file is being used; the HREF attribute tells the browser the name of the file, and where the file is. (You haven't done file referencing yet, so don't worry about HREF for the moment.)
External stylesheets have a big advantage over embedded ones because they can be used for all the pages in your web site. All you need do is insert the LINK tag in the HEAD section of your website's pages. With Embedded stylesheets, you would have to have the same STYLE code in the HEAD section of every page. External Stylesheets can save you a heck of a lot of typing (or copying and pasting).
External stylesheets are just text files, so you can write them in any text editor. Something like Windows Notepad is ideal. Here's a screenshot of an external stylesheet written with Notepad:
Note that no TAGS are used in the external stylesheet. There are no <STYLE> tags or HTML tags, just the Selectors and their Properties and Values.
Note, too, that three styles have been set up. You can do this with embedded STYLE tags as well. You can have as many different styles as you want between one pair of <STYLE> </STYLE> tags.
If you are using external stylesheets, you are not limited to only one. You can use as many external stylesheets as you need. All that is required is a different LINK tag for each stylesheet.
When saving your external stylesheets, you don't have to save it with the extension .css. You can save it with the extension .txt. In the HREF part of your LINK tag, you would then have this:
HREF = style1.txt
instead of
HREF = style1.css
In the next part, you'll learn how to use your CSS selectors.
How to use CSS Class and ID Selectors
You have seen how to set up a stylesheet. And you know that if you set up a HTML selector, then all the HTML tags will have their values reset with the new values you specified. For example if you had this:
Then all the H1 headings you used between the two BODY tags would have the text between the two <H1> tags coloured red.
In other words, to use your new HTML selector, you don't have to add anything else to your HTML code. Just use the tag in the normal way.
However, Class and ID selectors are slightly different.
Using Class Selectors
To set up a Class Selector, the code was this:
.ClassSelector {Property: Value}
First, you type a full stop (period). Then you type the name of your Class Selector (which can be anything you want). The Property and Value are then as normal. An example is this:
The .FontOne is the Class Selector, a name we made up ourselves. In between the curly brackets, we're then saying 'Every time this Rule is applied, the colour of the text will be red".
You can use this new Rule inside a normal HTML tag. Like this:
<H1 Class = "FontOne"> My Heading </H1>
When applying your new Class Selector Rule, the word 'Class' is used. You then type an equals sign ( = ), followed by the name you gave your Class Selector. In our case, this was FontOne . But note that the full stop (period) is now missing from the start of the selector name. If you put the full stop in, your Rule won't work. Note that although we've used quote marks around the Class name, this is not strictly necessary. It is recommended, though.
Using ID Selectors
You use an ID selector in exactly the same way that you use the Class selectors. The only difference is in the word used when applying the Rule. Instead of Class you use ID:
This is what the above code would look like in a web page:
But that's enough of the theory for now. Let's get some practical work done. We'll go through the various ways you can add a STYLE to your web pages using your text editor. The first code we're going to write will centre a heading on a page. You can then use the same code to centre anything on your page. Click below to get started.
<< Where to put styles | Inline and embedded styles >>
Inline and Embedded Styles
Inline Styles
Open up the HTML template text file you created at the start of the course. Click
File > Save As in Notepad. Save the file with the name newpage.html . Don't forget to change the Save as type box to All Files before clicking the Save button, if you're using Windows.
Add some text for the TITLE tag. Add an H1 heading, with some text between the two H1 tags. Now add a paragraph of text below the H1 heading. To create an Inline Style, click after the first H1. Tap the space bar on your keyboard. Type the following:
<H1 style=text-align:center >
Make sure there is no space after the colon and before the word 'center'.
Your HTML code should look something like ours below:
(One thing British English users should note is the two different spelling of the word 'centre'. American English is used in HTML, so it's 'er' rather than 're'.)
When you have finished typing the code, view the results in your browser. You should see that the heading is centred on your page:
So if you want to centre things on a page, you need to use the text-align property. After a colon, you add the value. In this case center .
(In previous versions of HTML you could use the HTML tags <CENTER> to centre things on a page. However, in the new version of HTML, version 5, this tag is now withdrawn (deprecated).
Exercise
As well as using center after the text-align: property you can use the following: left, right. Try them out for yourself. Simply replace the word center with one of the other text-align options.
Embedded Styles
As was mentioned, Embedded Styles go in the HEAD section of your code. To try them out, add the following highlighted CSS to your code:
So the text alignment we've added is to align right. However, the Inline Style is for centre alignment. So which one gets executed?
Save your work and refresh your browser page. You won't see any changes. That's because the Inline Style will override the embedded one in the HEAD section.
Delete the Inline Style for the H1 heading. You should then just have this for the H1 heading:
<H1> Centred Heading</H1>
Save your work and refresh the web page in your browser. It should look like this, now:
The heading is right-aligned.
Exercise
Change the value of the text-align property to Center. Save your work and Refresh the page in your browser.
In the next lesson, you'll learn about how to handle fonts with CSS.
CSS and Fonts
In HTML versions prior to the new version, which is HTML 5, you could specify a Font with HTML tags. If you wanted Arial, for example, you could do this:
<FONT face="Arial, Helvetica, san-serif"> Some text here </FONT>
However, In HTML 5 the FONT tag is deprecated. That doesn't mean you can't use it anymore, as browsers are backward compatible, meaning that older code will work with all browsers. But if you want to go with the modern way of doing things then fonts, font sizes, and font colours should be done with a Stylesheet.
To change the font on your page, the font-family CSS property is used:
font-family: arial, helvetica, sans-serif;
You specify the family because the person viewing your page might not have the first font on your list. In which case, the second font is tried, then the third. (You can have more than three, if you want. Or less than three.)
You can specify a single font instead, but not everybody has that fancy font you like so much. In which case, they would get the default, which is usually a serif font.
NOTE: A serif is mark or embellishment on the vertical or horizontal line of text. Here's an example of the serif font Times New Roman:
A sans-serif font is one without marks or embellishment. Just straight lines, in other words. Here's the sans-serif font Arial:
And here are some default font families you can use quite safely:
Times New Roman, Times, Serif
Courier New, Courier, Mono
Georgia, Times New Roman, Times, Serif
Verdana, Arial, Helvetica, Sans-Serif
Geneva, Arial, Helvetica, San-Serif
Try them out for yourself with the following font-family style added to your H1 code:
Save and refresh your browser. The heading should change from a default serif font to one of the sans-serif fonts: Arial, Helvetica, sans-serif.
But your browser should look like this:
Now try one of the other font-families on the list. Here's what your browser will look like if your try Courier New, Courier,
Mono :
In the next lesson, you'll learn about font colours.
Font Colors
We've produced a video to go with this lesson. It's recommended that you read the text below as well, though. The video is here:
Web Color Values
There are quite a lot of other font styles you can use in your CSS. We'll start with font colours. (Strictly speaking, though, COLOR is not a font style - it can be used just about anywhere to colour lots of different things.):
You can have just about any font colour you want for your text (British English users, note the missing "u" after the second "o". The American spelling is used in HTML and CSS). To specify a colour, you can use one of three formats:
The name of a colour spelled out
The name of a colour as a Hexadecimal value
The name of a colour as a RGB value
There is a rather long and exotic list of colour names you can use, things like Cadet Blue, Ghost White, Lemon Chiffon. But you can also use a simple colour name like red or blue. The CSS code is this:
H1 {
text-align: center;
font-family: Courier New, Courier, Mono;
color: red;
}
So you type the word "color" followed by a colon. You then type the name of your colour.
There are 17 common colours, and 130 not so common. The common colours you can use are: aqua, black, blue, fuchsia, gray, grey, green, lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow.
For a full list of all the colour names you can use, see this Wikipedia page:
Wikipedia - Web Colors
Hexadecimal values
Hexadecimal values are used quite a lot in web design. If you've never heard of them before they use the number 0 to 9, and the letters A to F (Base 16). In Base 16, the letters are used instead of the numbers 10, 11, 12, 13, 14 and 15. So A = 10, B=11, C=12, etc. If you want to represent the number 256 in Hexadecimal then it's FF.
Three colour values are used, one for Red, one for Green, and one for Blue. Each colour value has two Hexadecimal place-holders. For example, here's the colour Red:
#FF0000
Here's the colour Green:
#00FF00
And here's the colour Blue:
#0000FF
So red has two letter F's in the Red position, two zeros in the green position, and two zeros in the blue position. In other words, switch the red value full on, and switch the green and blue values off.
Green has two zeros, two F's, then two zeros again. Blue has four zeros then two F's.
Black just has 6 zeros, meaning switch Red, Green and Blue off.
Note that you need to use the hash/pound symbol ( # ) before the six place-holder values.
You can combine the Hexadecimal number and letter values:
#2F0B99
Try them out for yourself in your code:
H1 {
text-align: center;
font-family: Courier New, Courier, Mono;
color: #2F0B99;
}
Exercise
Change the values in the six positions, using the numbers 0 to 9, and the letters A to F. What are the following colours?
color: #1BAA12;
color: #1087AC;
color: #451DE2;
RGB values
You can use RGB values in your CSS, if you prefer. (RGB stands for Red, Green, Blue.) Again, place-holders are used for the different colour values. This time, you use the number 0 to 255. You use them like this:
color: RGB(255, 0, 0);
Note that the number are typed between a pair of round brackets, and are separated by comas.
Exercise
Play around with the RGB values. What are the following colours?
color: RGB(0, 255, 0);
color: RGB(125, 125, 0);
color: RGB(255, 255, 10);
Instead of experimenting to get the colour you want, an easy way to get the correct Hexadecimal or RGB colour is to type the words "color picker" into a search engine like Google. You'll then have plenty of web sites that give you're the correct values to type simply by playing around with colour sliders.
In the next lesson, you'll learn about font sizes.
Font Size with CSS
There are plenty of ways to change the font size with CCS.
You can change the size of your page fonts with the font-size property. The values for the property, however, can be a bit tricky. Take a look at these values for font-size:
font-size: 200%;
font-size: 32px;
font-size: 2em;
font-size: xx-large;
They are actually the same size! Try them out in your code. Add the following highlighted CSS Style:
Save your changes and refresh the page in your browser. You should see the paragraph of text change size.
Now try the other three sizes: 200%, 32px, and 2em. You should find that they are all the same size (or near enough).
Which one you use is up to you. In older browsers, however, using the fixed sizes values of px and em means your users may not be able to increase the size of your fonts. This matters to those whose eyesight may not be perfect, and therefore need a bigger font when reading your text. But browsers allow you to zoom in and out by holding down the CTRL key and pressing + or -. (You reset by pressing CTRL then 0.) So font-size values are not that important. A lot of professionals, though, will set the BODY tag's font-size to 100%, then use em as the value. Like this:
This means that all the font sizes between the two BODY tags are set to the browser's default size of 100%. In pixels, the default size in browsers is 16px. This is equal to 1em. The P tag above is setting the font-size to one and half times the default, while the class
ChangeFontSize is setting the font-size to two times the default.
The other sizes are seldom used. But here they are:
xx-small
x-small
small
medium
large
x-large
xx-large
smaller
larger
Try them out and see what happens.
In the next part, you'll learn about styling fonts.
<< Font Colours | Styling Fonts >> Font Size with CSS
There are plenty of ways to change the font size with CCS.
You can change the size of your page fonts with the font-size property. The values for the property, however, can be a bit tricky. Take a look at these values for font-size:
font-size: 200%;
font-size: 32px;
font-size: 2em;
font-size: xx-large;
They are actually the same size! Try them out in your code. Add the following highlighted CSS Style:
Save your changes and refresh the page in your browser. You should see the paragraph of text change size.
Now try the other three sizes: 200%, 32px, and 2em. You should find that they are all the same size (or near enough).
Which one you use is up to you. In older browsers, however, using the fixed sizes values of px and em means your users may not be able to increase the size of your fonts. This matters to those whose eyesight may not be perfect, and therefore need a bigger font when reading your text. But browsers allow you to zoom in and out by holding down the CTRL key and pressing + or -. (You reset by pressing CTRL then 0.) So font-size values are not that important. A lot of professionals, though, will set the BODY tag's font-size to 100%, then use em as the value. Like this:
This means that all the font sizes between the two BODY tags are set to the browser's default size of 100%. In pixels, the default size in browsers is 16px. This is equal to 1em. The P tag above is setting the font-size to one and half times the default, while the class
ChangeFontSize is setting the font-size to two times the default.
The other sizes are seldom used. But here they are:
xx-small
x-small
small
medium
large
x-large
xx-large
smaller
larger
Try them out and see what happens.
In the next part, you'll learn about styling fonts.
<< Font Colours | Styling Fonts >>
Font Style, Font Weight, Font Variant
Font-Style
There are only three font styles to choose from: normal, italic, and oblique. Of course, the default is normal, so you don't have to use font-style unless you want italic or oblique text. You use them like this:
There's not a great deal of difference between italic and oblique, though. If you want to see the difference, use a sans-serif font, with a large font size.
Font-Variant
Font-Variant is used when you want small caps for text:
And here's what it looks like in a browser:
Font-Weight
The weight of a font is how thick it is. There are lots of values you can use here. The most common one is Bold. But you can also have Bolder and Lighter. As well as the named weight you can also use a number from 100 to 900. It's better to stick with round numbers, though, as the difference between 100 and 150 is practically impossible to see. Here's how to use font-weight:
Add a second paragraph of text between your BODY tags and try it out:
Here, we've set all paragraph tags to have bold text. Then we've added a Class Selector called Change_Font_Weight , which we've set to lighter . We've then used this class on the second paragraph.
Save your work and refresh your browser to see what effect it has.
Exercise
Try some of the number font weights to see how they work. How different is 100 from 900?
We'll take a break from CSS, now, but come back to it later. In the next section, you'll learn how to insert images into a web page.
CSS Font and Text Properties
CSS Property Example Value Other Values
text-align: center Left, Right
font-family: Verdana, Arial, Helvetica, Sans-Serif
font-size: 16px |percent |em |xx-large, x-small, small, medium, large, x-large, smaller, larger
font-style: italic normal (the default), oblique
font-variant: small-caps
font-weight: Bold Bolder | Lighter | Any number from 100 to 900
Color: Red A colour name, Hexadecimal value, RGB Value